// components/memberInfo/memberInfo.js
//用于检测用户是否授权头像信息
//未授权的情况下弹出授权
import * as API_Member from '../../api/member'
import * as API_Config from '../../api/sysConfig'
import * as API_Upload from '../../utils/fileUpload'
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    //用户信息
    userInfo: {},
    scrollHeight: '',
    scrollTop: 0,//滚动高度
    //授权弹窗
    showDialog: true,
    timmer:'',
    showInput: false,
    avatarUrl: '',
    nickName: '',
    showClose: false
  },
  lifetimes:{
    attached(){
      let that = this
      //获取设备宽高
      this.setData({scrollHeight: wx.getSystemInfoSync().windowHeight});
      API_Config.getValueByKey("member_avatar").then(res =>{
        if('Y' == res){
          that.startCheck()
        }
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    closeSubmit () {
      this.setData({
        showDialog: true,
        showInput: false,
        showClose: false
      })
    },
    onChooseAvatar (e) {
      const { avatarUrl } = e.detail
      API_Upload.upload(avatarUrl, res => {
        const { url } = JSON.parse(res.data)
        this.setData({
          avatarUrl: url
        })
      }, () => {
        wx.showToast({
          title:'上传失败',
          icon:'none'
        })
      })
    },
    startCheck(){
      let that = this
      //默认必须有token
      let token = wx.getStorageSync('token')
      //当没有的时候设置定时器等待token返回
      if(!token){
        clearInterval(this.data.timmer)
        this.data.timmer= setInterval(
          function () {
            wx.showLoading({
              title: '加载中...',
              mask:true
            })
            let d = wx.getStorageSync('token')
            if(d){
              wx.hideLoading()
              that.check()
            }
          }, 500);
      }else{
        that.check()
      }
    },
    check(){
       clearInterval(this.data.timmer)

       const member = wx.getStorageSync('member')

       if(!member.headImg || !member.nickName){
          wx.hideTabBar()
          this.setData({
            showDialog:false
          })
        // API_Member.reload().then(res=>{
        //   if(!res.headImg || !res.nickName){
        //     wx.hideTabBar()
        //      this.setData({
        //        showDialog:false
        //      })
        //    }
        // })
       }
      
    },
    cancel:function(){
      
      let pages=getCurrentPages();
      wx.showTabBar({
        animation: true,
      })
      if(pages.length>1){
        wx.navigateBack()
      }else{
        wx.switchTab({
          url: '/pages/home/home',
        })
      }
     
    },
    setAvatarNickname (e) {
      let that = this
      let formData = e.detail.value
      let storeUserId = wx.getStorageSync('storeUserId')
      let bindMemberId = wx.getStorageSync('bindMemberId')
      let storeId = wx.getStorageSync('storeId')
      let uInfo = {}
      if (!formData.headImgUrl) {
        wx.showToast({
          title: '请上传头像！',
          icon: 'none'
        })
        return
      }
      if (!formData.nickName) {
        wx.showToast({
          title: '请输入昵称！',
          icon: 'none'
        })
        return
      }
      if (this.data.showClose) {
        let member = wx.getStorageSync('member')
        uInfo = {
          headImg: formData.headImgUrl,
          nickName: formData.nickName,
          id: member.id,
          parentMemberId: bindMemberId,
          storeUserId: storeUserId
        }
        getApp().https.ajax({
          method: 'post',
          url: '/member/updateMember',
          data: uInfo,
          loading: true,
          isJson: true
        }).then(res => {
          if (res.code === 200) {
            wx.showToast({
              title: res.msg,
              icon: 'none'
            })
            member.headImg = formData.headImgUrl
            member.nickName = formData.nickName
            wx.setStorageSync('member', member)
            this.closeSubmit()
            that.triggerEvent('MemberInfo')
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none'
            })
          }
        })
      } else {
        uInfo = {
          headImgUrl: formData.headImgUrl,
          nickName: formData.nickName,
          storeUserId: storeUserId,
          bindMemberId: bindMemberId,
          storeId:storeId
        }
        API_Member.updateBasicInfo(uInfo).then(res => {
          wx.setStorageSync('member', res.data);
          that.setData({
            userInfo:res.data,
            showDialog:true
          })
          var close =  getApp().globalData.phone
          if(!close){
            wx.showTabBar({
              animation: true,
            })
          }
          var detail = {} 
          var option = {} 
          that.triggerEvent('MemberInfo', detail, option)
        })
      }
    },
    userProfile:function(){
      const that = this
      this.setData({
        showInput: true
      })
      return
      wx.getUserProfile({
        desc:'获取信息',
        success(res){
          const userInfo = res.userInfo
          let uInfo = that.data.userInfo;
          let storeUserId = wx.getStorageSync('storeUserId')
          let bindMemberId = wx.getStorageSync('bindMemberId')
          uInfo = {
            headImgUrl:userInfo.avatarUrl,
            nickName:userInfo.nickName,
            storeUserId: storeUserId,
            bindMemberId: bindMemberId
          }
          console.log(uInfo);
           //更新用户信息
           API_Member.updateBasicInfo(uInfo).then(res => {
            wx.setStorageSync('member', res.data);
            that.setData({
              userInfo:res.data,
              showDialog:true
            })
            var close =  getApp().globalData.phone
            if(!close){
              wx.showTabBar({
                animation: true,
              })
            }
            var detail = {} 
            var option = {} 
            that.triggerEvent('MemberInfo', detail, option)
          })
          
        },
        fail(res){
          wx.switchTab({
            url: '/pages/home/home',
          })
        }
      })
  
    },
  }
})
